package GBP;

import java.io.*;
import java.util.*;

/**
 * ReadIni.java
 * Created: 2006-01-05 23:30
**/

/**
 * This class is used to read in the parameters
 * for the Graph-Bipartitioning-Problem experiment.
 *
 * @author T.S.Yo
**/

public class ReadIni{
    public int nNode;         // Number of nodes
    public String gFile;      // File keeps definition of the graph
    public int popSize;       // Size of population
    public int optFit;        // Fitness of known optimum
    public int maxGen;        // Maximum generation to evolve
    public String oFile;      // File keeps output

// Constructor    
    /** File name specified **/
    public ReadIni(final String pFile){
        ReadParam(pFile);
    }
    /** Use default file name **/
    public ReadIni(){
        ReadParam("gbp.ini");
    }

// Reading parameter: 
//   read from the given file, skip lines start with #, then parse the parameters
    /** Open given file, then send lines not start with # to parseParam() **/
    public void ReadParam(final String fName){
        try
        {
            String line;
            BufferedReader br = new BufferedReader(new FileReader(fName));         
            while ((line = br.readLine()) != null) {
                if(!line.startsWith("#")){
                    //System.out.println(line);             // For testing
                    parseParam(line);
                }
            }
            br.close();
            br = null;       
        } catch (java.io.FileNotFoundException ex) {
            System.out.println("!!!    File '" + fName + "' does not exist    !!!");
        } catch (java.io.IOException ex) {
            ex.printStackTrace();
        } 
    }

    /** Parse line to retreive parameters **/
    public void parseParam(final String line){
        // Name and values of a paramter
        String pName;
        String pValue;
        
        // Tokenize the node data
        StringTokenizer st = new StringTokenizer(line, " =");
        
        // 1st token: Parameter Name
        pName = st.nextToken();
        // 2nd token: Parameter Value
        pValue = st.nextToken();
        
        // Set parameters
        if (pName.startsWith("n_node")){
            System.out.println("    Number of node:    "+pValue);
            nNode = Integer.parseInt(pValue);
        } else if (pName.startsWith("g_def")){
            System.out.println("    Graph definition file:    "+pValue);
            gFile = pValue;
        } else if (pName.startsWith("p_size")){
            System.out.println("    Size of population:    "+pValue);
            popSize = Integer.parseInt(pValue);
        } else if (pName.startsWith("o_fit")){
            System.out.println("    CutSize of optimum:    "+pValue);
            optFit = Integer.parseInt(pValue);
        } else if (pName.startsWith("m_gen")){
            System.out.println("    Maximum generation:    "+pValue);
            maxGen = Integer.parseInt(pValue);
        } else if (pName.startsWith("o_file")){
            System.out.println("    Output file:    "+pValue);
            oFile = pValue;
        }
    }
}
